package com.xstablet.view.popuputil;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.ui.adapter.BaseAdapter;
import com.xstablet.R;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 用于筛选年级和科目的弹出框
 * <p>
 * <p>
 * Created by Administrator on 2020/8/3 0003.
 */

public class ConditionChoosePopup {


    private Activity activity;

    ConditionChooseAdapter listAdapter;
    List<String> chooseList = new ArrayList<>();

    public ConditionChoosePopup(Activity activity) {

        this.activity = activity;

        initPopuPopu();
    }

    private EasyPopup popu;

    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.popup_conditionchoose)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);
                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }

    private RecyclerView rvChoose;

    private void initView(View view) {

        rvChoose = view.findViewById(R.id.rv_choose);

        listAdapter = new ConditionChooseAdapter(activity, chooseList);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(activity);
        rvChoose.setLayoutManager(manager);
        rvChoose.setAdapter(listAdapter);

    }

    public void updateChooseData(List<String> condiList) {

        if (chooseList != null) {
            if (chooseList.size() > 0) {
                chooseList.clear();
            }
            chooseList.addAll(condiList);
            listAdapter.notifyDataSetChanged();
        }

    }


    public void dismiss() {
        popu.dismiss();
    }


    public void showAnswerPopuPopu(View view) {
        popu.showAtLocation(view, Gravity.CENTER, 0, 0);

    }

    public class ConditionChooseAdapter extends BaseAdapter<String> {

        public ConditionChooseAdapter(Context context, List<String> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_choose;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<String> mData) {

            holder.setText(R.id.tv_condition, mData.get(position));

            LinearLayout linItem = holder.getView(R.id.lin_item);
            linItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onChoose(mData.get(position));
                    }
                }
            });

        }
    }


    public interface ConditionChoosedListener {

        void onChoose(String condition);
    }

    public ConditionChoosedListener listener;

    public void setConditionChoosedListener(ConditionChoosedListener conditionChoosedListener) {
        this.listener = conditionChoosedListener;
    }

}
